/*
ID:luongho3
LANG:C++
TASK:clocks
*/
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin("clocks.in");
ofstream fout("clocks.out");
#define NMOVE 9
#define SIZE 3
#define NSTATE 263000
struct conf {
       int m[SIZE][SIZE];
       int id;
};
int hash(conf c) {
    int i,row,col,tmp,val;
    tmp = 1;
    val = 0;
    for (i=0;i<SIZE*SIZE;++i) {
        row = i/SIZE;
        col = i%SIZE;
        c.m[row][col] = (c.m[row][col]/3 - 1);
        val += (tmp * c.m[row][col]);
        tmp *= 4;
    }
    return val;
}
typedef queue<conf> cQueue;
int moves[NMOVE][SIZE][SIZE]={
    {{3,3,0},{3,3,0},{0,0,0}},
    {{3,3,3},{0,0,0},{0,0,0}},
    {{0,3,3},{0,3,3},{0,0,0}},
    {{3,0,0},{3,0,0},{3,0,0}},
    {{0,3,0},{3,3,3},{0,3,0}},
    {{0,0,3},{0,0,3},{0,0,3}},
    {{0,0,0},{3,3,0},{3,3,0}},
    {{0,0,0},{0,0,0},{3,3,3}},
    {{0,0,0},{0,3,3},{0,3,3}}   
    };
conf move(conf c, int mt) {
     int i,j;
     for (i=0;i<SIZE;++i) {
         for (j=0;j<SIZE;++j) {
             c.m[i][j] = (c.m[i][j] + moves[mt][i][j]) % 12;
             if (c.m[i][j]==0) {
                c.m[i][j] = 12;
             }
         }
     }
     c.id = hash(c);
     return c;
}
bool isfinal(conf c) {
     int i,j;
     for (i=0;i<SIZE;++i) {
         for (j=0;j<SIZE;++j) {
             if (c.m[i][j] != 12) {
                return false;
             }
         }
     }
     return true;
}
conf init,current,temp;
int prev[NSTATE],pmove[NSTATE],path[NSTATE],plen;
cQueue qu;
int main() {
    int i,j;
    for (i=0;i<SIZE;++i) {
        for (j=0;j<SIZE;++j) {
            fin >> init.m[i][j];
        }
    }
    init.id = hash(init);
    for (i=0;i<NSTATE;++i) {
        prev[i] = -1;
    }
    qu.push(init);
    prev[hash(init)] = 0;
    while (!qu.empty()) {
          current = qu.front();
          qu.pop();
          if (current.id==262143) {
             break;
          }
          for (i=0;i<NMOVE;++i) {
              temp = move(current,i);
              if (prev[temp.id]==-1) {
                 j = hash(temp);
                 qu.push(temp);
                 pmove[j] = i;
                 prev[j]=current.id;
              }
          }
    }
    plen=0;
    current.id = hash(current);
    i = current.id;
    while (prev[i]!=0) {
          plen++;
          path[plen] = pmove[i];
          i = prev[i];;
    }
    for (i=plen;i>=1;--i) {
        fout <<path[i]+1;
        if (i>1) {
           fout << " ";
        }
        else {
             fout << "\n";
        }
    }
    return 0;
}
